package com.ruoyi.mp.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.service.IMpAccountService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.draft.WxMpAddDraft;
import me.chanjar.weixin.mp.bean.draft.WxMpUpdateDraft;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <h3>springcloudSaas</h3>
 * <p>草稿箱</p>
 *
 * @author : 郎兴宇
 * @date : 2024-09-29 01:00
 **/
@RestController
@RequestMapping("/draft")
public class MpDraftController extends BaseController {
    @Autowired
    private IMpAccountService iMpAccountService;
    @Autowired
    private WxMpService wxService;
    /**
     * 新增草稿
     */
    @RequiresPermissions("mp:draft:add")
    @Log(title = "新增草稿", businessType = BusinessType.INSERT)
    @PostMapping("/{id}")
    public AjaxResult addDraft(@PathVariable Long id, WxMpAddDraft addDraft) throws WxErrorException {

        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        wxService.switchoverTo(appId).getDraftService().addDraft(addDraft);
        return AjaxResult.success();
    }

    /**
     * 获取草稿数量
     */
    @RequiresPermissions("mp:draft:countDraft")
    @Log(title = "获取草稿数量", businessType = BusinessType.INSERT)
    @GetMapping("/countDraft/{id}")
    public AjaxResult countDraft(@PathVariable Long id) throws WxErrorException {

        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        wxService.switchoverTo(appId).getDraftService().countDraft();
        return AjaxResult.success();
    }


    /**
     * 删除草稿
     */
    @RequiresPermissions("mp:draft:delDraft")
    @Log(title = "删除草稿", businessType = BusinessType.DELETE)
    @PostMapping("/delDraft/{id}")
    public AjaxResult 	delDraft(@PathVariable Long id) throws WxErrorException {

        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String mediaId = "";
        wxService.switchoverTo(appId).getDraftService().delDraft(mediaId);
        return AjaxResult.success();
    }



    /**
     * 获取草稿信息
     */
    @RequiresPermissions("mp:draft:getDraft")
    @Log(title = "获取草稿信息", businessType = BusinessType.EXPORT)
    @PostMapping("/getDraft/{id}")
    public AjaxResult getDraft(@PathVariable Long id) throws WxErrorException {

        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String mediaId = "";
        wxService.switchoverTo(appId).getDraftService().getDraft(mediaId);
        return AjaxResult.success();
    }




    /**
     * 获取草稿列表
     */
    @RequiresPermissions("mp:draft:listDraft")
    @Log(title = "获取草稿列表", businessType = BusinessType.EXPORT)
    @PostMapping("/listDraft/{id}")
    public AjaxResult listDraft(@PathVariable Long id) throws WxErrorException {

        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        int offset = 0; int count=20;int noContent = 50;
        wxService.switchoverTo(appId).getDraftService().listDraft(offset, count,noContent);
        return AjaxResult.success();
    }


    /**
     * 修改草稿
     */
    @RequiresPermissions("mp:draft:updateDraft")
    @Log(title = "修改草稿", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDraft/{id}")
    public AjaxResult updateDraft(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        WxMpUpdateDraft updateDraftInfo = new WxMpUpdateDraft();
        wxService.switchoverTo(appId).getDraftService().updateDraft(updateDraftInfo);
        return AjaxResult.success();
    }
}


